package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.exceptions.AuthException;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.entity.DataDictionaryCategory;
import eteam.aps.model.vo.*;
import eteam.aps.service.DataDictionaryCategoryService;
import eteam.aps.service.DataDictionaryService;
import eteam.aps.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典分类控制器层
 *
 * @author lizhi
 */
@RestController
@Api(tags = "字典分类模块")
public class DataDictionaryCategoryController implements IDataDictionaryCategoryController {

    @Autowired
    private DataDictionaryCategoryService dataDictionaryCategoryService;

    @Autowired
    private DataDictionaryService dataDictionaryService;

    @Autowired
    private UserService userService;

    @Override
    @ApiOperation("新增字典分类")
    @PostMapping("/ddc/insert")
    public R<Integer> insert(@RequestBody DataDictionaryCategory category) throws Exception, AuthException {
        if (Helper.isEmpty(category.getfDdcCode()) || Helper.isEmpty(category.getfDdcName())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "编号或名称不能为空");
        }
        if (dataDictionaryCategoryService.selectCountByFDdcCode(category.getfDdcCode()) > 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该编号已经存在");
        }
        if (dataDictionaryCategoryService.selectCountByFDdcName(category.getfDdcName()) > 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该名称已经存在");
        }
        return R.success(dataDictionaryCategoryService.insert(category));
    }

    @Override
    @ApiOperation("删除字典分类")
    @PostMapping("/ddc/delete/{fId}")
    public R<Integer> delete(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "没有字典分类内码");
        }

        if (dataDictionaryCategoryService.selectCountByFId(fId) == 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典分类不存在");
        }

        if (dataDictionaryCategoryService.selectByFState(fId) == ParentEntity.FSTATE_DELETED){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "字典分类已被删除");
        }

        if (dataDictionaryService.selectCountByFDdcId(fId) > 0) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典分类下有字典，不能删除");
        }
        return R.success(dataDictionaryCategoryService.delete(fId));
    }

    @Override
    @ApiOperation("修改字典分类")
    @PostMapping(value = "/ddc/update")
    public R<Integer> update(@RequestBody DataDictionaryCategory category) throws Exception, AuthException {
        if (Helper.isEmpty(category.getfId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "没有字典分类内码");
        }
        if (Helper.isEmpty(category.getfDdcCode()) || Helper.isEmpty(category.getfDdcName())
                || Helper.isEmpty(category.getfDdcManagerId())) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "编号,名称或字典管理人不能为空");
        }
        DataDictionaryCategoryUpdateVo updateVo = dataDictionaryCategoryService.selectUpdate(category.getfId());
        if (updateVo == null) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典分类不存在");
        }

        if (!updateVo.getfDdcCode().equals(category.getfDdcCode())) {
            if (dataDictionaryCategoryService.selectCountByFDdcCode(category.getfDdcCode()) > 0) {
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该分类编码已经存在");
            }
        }
        if (!updateVo.getfDdcName().equals(category.getfDdcName())) {
            if (dataDictionaryCategoryService.selectCountByFDdcName(category.getfDdcName()) > 0) {
                throw new BusinessException(HttpStatus.BAD_REQUEST, "该分类名称已经存在");
            }
        }
        return R.success(dataDictionaryCategoryService.update(category));
    }

    @Override
    @ApiOperation("查询字典分类详情")
    @PostMapping(value = "/ddc/selectDetails/{fId}")
    public R<DataDictionaryCategoryDetailsVo> selectDetails(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "没有字典分类内码");
        }
        DataDictionaryCategoryDetailsVo detailsVo = dataDictionaryCategoryService.selectByFId(fId);
        if (detailsVo == null) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典分类不存在");
        }
        return R.success(detailsVo);
    }

    @Override
    @ApiOperation("查询字典分类可修改信息")
    @PostMapping(value = "/ddc/selectUpdate/{fId}")
    public R<DataDictionaryCategoryUpdateVo> selectUpdate(@PathVariable String fId) throws Exception, AuthException {
        if (Helper.isEmpty(fId)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "没有字典分类内码");
        }
        DataDictionaryCategoryUpdateVo category = dataDictionaryCategoryService.selectUpdate(fId);
        if (category == null) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "该字典分类不存在");
        }
        return R.success(category);
    }

    @Override
    @ApiOperation("批量查询字典分类")
    @PostMapping("/ddc/selectAll")
    public R<JsonPage<DataDictionaryCategoryBasicsVo>> selectAll(
            @RequestParam(value = "fDdcCode", required = false) String fDdcCode,
            @RequestParam(value = "fDdcName", required = false) String fDdcName,
            @RequestParam("page") Integer page,
            @RequestParam("pageSize") Integer pageSize) throws Exception, AuthException {
        Map<String, Object> map = new HashMap<String, Object>(4);
        if (Helper.isNotEmpty(fDdcCode)) {
            map.put("fDdcCode", Helper.likeSql(fDdcCode));
        }
        if (Helper.isNotEmpty(fDdcName)) {
            map.put("fDdcName", Helper.likeSql(fDdcName));
        }
        return R.success(dataDictionaryCategoryService.selectAll(map, Helper.valuePositive(page, 1),
                Helper.valuePositive(pageSize, 10)));
    }

    @Override
    @ApiOperation("批量查询字典可选择分类")
    @PostMapping("/ddc/selectAllInquire")
    public R<List<DataDictionaryCategoryInquireVo>> selectAllInquire(
            @RequestParam(value = "fDdcName", required = false) String fDdcName,
            @RequestParam("pageSize") Integer pageSize) throws Exception, AuthException {
        Map<String, Object> map = new HashMap<String, Object>(2);
        if (Helper.isNotEmpty(fDdcName)) {
            map.put("fDdcName", Helper.likeSql(fDdcName));
        }
        map.put("limit", Helper.valuePositive(pageSize, 10));
        return R.success(dataDictionaryCategoryService.selectAllInquire(map));
    }

    @Override
    @ApiOperation("查询所有用户信息")
    @PostMapping("/ddc/selectAllUser")
    public R<List<UserVo>> selectAllUser(@RequestParam(value = "fullName", required = false) String fullName) throws Exception, AuthException {
        Map<String, Object> map = new HashMap<>();
        if (Helper.isNotEmpty(fullName)){
            map.put("fullName",Helper.likeSql(fullName));
        }
        return R.success(userService.selectAll(map));
    }
}